package com.organicsalt.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.organicsalt.springboot.enitty.Statement;

import com.organicsalt.springboot.service.StatementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/statement")
public class StatementController {

    @Autowired
    private StatementService statementService;

    // 新增和修改
    @PostMapping("/save")
    public boolean save(@RequestBody Statement statement) {
        // 新增或者更新
        return statementService.saveOrUpdate(statement);
    }

    // 查询所有数据
    @GetMapping
    public List<Statement> findAll() {
        return statementService.list();
    }

    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id) {
        return statementService.removeById(id);
    }

    // 分页查询 - mybatis-plus的方式
    @GetMapping("/page")
    public IPage<Statement> findPage(@RequestParam Integer pageNum,
                                     @RequestParam Integer pageSize,
                                     @RequestParam(defaultValue = "") String event,
                                     @RequestParam(defaultValue = "") String date,
                                     @RequestParam(defaultValue = "") String eq1,
                                     @RequestParam(defaultValue = "") String eq2) {
        IPage<Statement> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Statement> queryWrapper = new QueryWrapper<>();
        if (!"".equals(date)) {
            queryWrapper.like("date", date);
        }
        if (!"".equals(event)) {
            queryWrapper.like("event", event);
        }
        if (!"".equals(eq1)) {
            queryWrapper.ge("payment", eq1);
        }
        if (!"".equals(eq2)) {
            queryWrapper.le("payment", eq2);
        }
        queryWrapper.orderByDesc("id");
        return statementService.page(page, queryWrapper);
    }

    //根据id批量删除
    @PostMapping("/del/batch/")
    public boolean deleteBatch(@RequestBody List<Integer> ids) {
        return statementService.removeByIds(ids);
    }
}
